package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
* 购物车功能
* */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
    * 添加菜品或套餐
    * */
    @PostMapping("/add")
    public R<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}",shoppingCart);
        //设置用户id，指定是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();

        //添加公共判断条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);

        //判断添加的是菜品还是套餐
        if (dishId != null) {
            //添加的是菜品
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        } else {
            //添加的是套餐
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //查询菜品或者套餐id是否已经在购物车
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if (one!=null) {
            //如果存在该菜品（套餐），则在数量上加一
            Integer number = one.getNumber();
            number = number + 1;
            one.setNumber(number);
            shoppingCartService.updateById(one);
        } else {
            //如果不存在该菜品（套餐），则进行添加操作，菜品数据默认为一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            //将返回的数据统一
            one = shoppingCart;
        }
        return R.success(one);
    }

    /**
    * 根据用户（UserId）查询购物车数据
    * */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车数据：");

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);

        return R.success(list);
    }

    /**
    * 清空购物车(根据用户id)
    * */
    @DeleteMapping("/clean")
    public R<String> clean() {
        log.info("清空购物车...");

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(lqw);

        return R.success("清空购物车成功");
    }

    /**
     * 删除菜品或套餐
     * */
    @PostMapping("/sub")
    public R<String> delete(@RequestBody ShoppingCart shoppingCart) {
        log.info("删除菜品数据:{}",shoppingCart);
        //设置用户id，指定是哪个用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //判断删除的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        if (dishId != null) {
            //要删除的是菜品数据
            lqw.eq(ShoppingCart::getDishId,dishId);
        } else {
            //要删除的数据是套餐
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //根据菜品id查询对应菜品的数量
        ShoppingCart one = shoppingCartService.getOne(lqw);
        Integer number = one.getNumber();

        if (number > 1) {
            //如果菜品数量（number）大于1，则修改该菜品（套餐）number减1
            one.setNumber(number-1);
            shoppingCartService.updateById(one);
        } else {
            //如果数量小于或者等于一，则进行删除操作
            shoppingCartService.remove(lqw);
        }

        return R.success("删除数据成功 ");


    }


}
